package com.burgeon.backend.EnterpriseWeChat.controller;

import com.burgeon.backend.EnterpriseWeChat.entity.EWechat;
import com.burgeon.backend.EnterpriseWeChat.entity.YCOrder;
import com.burgeon.backend.EnterpriseWeChat.mappers.EWInfoMapper;
import com.burgeon.backend.EnterpriseWeChat.service.EWEmpService;
import com.burgeon.backend.EnterpriseWeChat.service.EWInfoService;
import com.burgeon.backend.stsutil.ResponseResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@RestController
@RequestMapping("ew/info")
public class EWInfoController extends EWBaseController {
    private final static Log logger = LogFactory.getLog(EWInfoController.class);
    private final static String CODEOK =
            "<xml><return_code><![CDATA[SUCCESS]]></return_code>" +
                    "<return_msg><![CDATA[OK]]></return_msg></xml>";

    @Autowired
    private EWInfoService ewInfoService;
    @Autowired
    private EWInfoMapper ewInfoMapper;
    @Autowired
    private EWEmpService ewEmpService;

    /**
     * 测试微信企业号第三方应用回调协议 xx
     *
     * @param req
     * @param res
     * @author:leap
     * @MethodName: testWXSaaSCallback
     * @Description:
     * @date:2016-8-9
     */
    @RequestMapping("WXSaaSCallback/{token}")
    public void WXSaaSCallback(HttpServletRequest req, ServletResponse res,
                               @PathVariable("token") String token) {

        EWechat eWechat = ewInfoMapper.findEWechat(token);

        String CORP_ID = eWechat.getCorpid(); //CorpId, 企业号的普通管理组中可查看"ww6e5e29ebac488932"
        /*String SUITE_ID = "tja2312bedd5086xxx";    //套件ID，在套件信息中查看
        String SUITE_SECRET = "LGVT0BrN2DZ7VMTuIPOsudaWuXOE0iM67yHpIs1ofESS4l-jSC8LN9nOnmilhXXX";*/
        String SUITE_TOKEN = eWechat.getSuiteToken();//"NjAQhsPPH58u7yLCQ9bs9BYF2m"
        String SUITE_ENCODING_AES_KEY = eWechat.getEncodingAESKey();//"ylBhpofwOAQGbf4FdOB7zbjk3aAlZRBSIEdvJXTEiAv"

        //获取参数
        String msgSignature = req.getParameter("msg_signature");
        String timestamp = req.getParameter("timestamp");
        String nonce = req.getParameter("nonce");
        String echostr = req.getParameter("echostr");    //创建套件时验证回调url时传入


        ewEmpService.WXSaaSCallback(req, res, CORP_ID, SUITE_TOKEN, SUITE_ENCODING_AES_KEY,
                msgSignature, timestamp, nonce, echostr,token);

    }



    //接收 suite_ticket  xx
    @RequestMapping("suiteTicket")
    public void suiteTicket(@RequestBody String code, HttpServletResponse response) {
        try {
            wxpayCallbackResult(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //**
        ewInfoService.suiteTicket(code);


    }


    //获取 suite_access_token

    private void wxpayCallbackResult(HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/xml; charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(CODEOK);
        out.close();
    }

    /**
     * 生成联系我企业微信二维码
     * @param token
     * @param empNo
     * @return
     */
    @RequestMapping("qyContactMeCode")
    public ResponseResult<String> qyContactMeCode(String token, String empNo){
        System.err.println(empNo);
        String data = ewInfoService.qyContactMeCode(token,empNo);
        return new ResponseResult<>(SUCCESS,data);
    }



}
